package sy.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.AppFriendsDaoI;
import sy.model.AppFriends;
@Repository("appFriendsDao")
public class AppFriendsDaoImpl implements AppFriendsDaoI{
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}

	@Override
	public List<AppFriends> listAppFriendsByAccountId(String accountId) {
		// TODO Auto-generated method stub
		String hql = "from AppFriends where sysAccount.id = :accountId";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("accountId", accountId);
		return query.list();
	}

	@Override
	public List<AppFriends> listAppFriendsByProjectId(String projectId) {
		// TODO Auto-generated method stub
		String hql = "from AppFriends where 1=1 ";
		 List<AppFriends> list = null;
		/**
		 * 一开始不考虑同一个项目才能看到对应的朋友圈动态
		 * 传过来的projectId没有值，故作以下判断
		 */
		if (!"".equals(projectId) && projectId != null) {
			hql += " and sysProject.id = :projectId";
			list = this.getCurrentSession().createQuery(hql).setString("projectId", projectId).list();
		} else {
			list = this.getCurrentSession().createQuery(hql).list();
		}
		return list;
	}

	@Override
	public void save(AppFriends appFriends) {
		// TODO Auto-generated method stub
		this.getCurrentSession().save(appFriends);
	}

	@Override
	public void delete(String id) {
		// TODO Auto-generated method stub
		String hql = "delete from AppFriends where id = :id";
		Query query = this.getCurrentSession().createQuery(hql);
		query.setString("id", id);
		query.executeUpdate();
	}

}
